#!/usr/bin/perl
use JSON;
use Getopt::Long;
use Term::ANSIColor;
#use Data::Dumper;

my $git_cfg_file         = "";
my $opt_help             = 0;
my $opt_file             = "";
my $cmp_dir              = "./build/comp";
my $cmp_opt              = "";
my $options_number = scalar(@ARGV);
my $sum_log_name = "rtl_sum.log";
my $fpga_flist   = 0;
my $fpga_version = "NoFPGAVersion";
my $opt_success = GetOptions(
  'h|help!'    => \$opt_help,
  'f|file=s'   => \$opt_file,
  'c|cfg=s'    => \$git_cfg_file,
  'cmp_dir=s'  => \$cmp_dir,
  'cmp_opt=s'  => \$cmp_opt,
  'sum_log=s'  => \$sum_log_name,
  'fpga=s'     => \$fpga_version,
  'fpga!'      => \$fpga_flist,
) or usage();
if($fpga_version ne "NoFPGAVersion") {
  $fpga_flist = 1;
}

if($fpga_flist == 1) {
  print "Use FPGA Version STAR_DPC!!!\n";
} else {
  print "Use ASIC Version STAR_DPC!!!\n";
}
if($opt_help || $options_number==0) {usage();}

if(!-d $cmp_dir) {
  print "Create Dir : $cmp_dir\n";
  $return_status = system("mkdir -p $cmp_dir");
  if($return_status) {
    print "Create $cmp_dir fail!!!\n";
    exit(1);
  }
}


my $json         = JSON->new->utf8;
my @rtl_file_list_queue = split (' ', $opt_file);
my @stored_log_file_list;

if($git_cfg_file ne "") {
  my $fh;
  if(!open ($fh, '<', $git_cfg_file)) {
    print "Can't open $git_cfg_file:$!\n"; 
    exit(1);
  }
  local $/;
  my $json_text = <$fh>;
  my $perl_scalar = $json->decode($json_text);
  close ($fh);
  my @warn_msg_queue;
  my %dst_full_path;
  local $/="\n";
  
  #print Dumper(\%ENV);
  foreach my $keys (keys %{$perl_scalar}) {
    my $block_file_list = "$keys.f";
    my @valid_file_list = ();
    my $curr_cmp_opt = "";
    print color('bold green on_black');
    printf("[%-s]:\n", $keys);
    print color('reset');
    print color('clear');
    if(exists ${$perl_scalar}{$keys}{"MY-DST"}) {
      $dst_full_path{$keys} = &swap_env_path(${$perl_scalar}{$keys}{"MY-DST"});
    } else {
      print " \"MY-DST\" of $keys must be set in the git configuration file!\n";
      exit(1);
    }
    if(!exists $ENV{"path_$keys"}) {
      $ENV{"path_$keys"} = $dst_full_path{$keys};
    }
    if(exists ${$perl_scalar}{$keys}{"MY-CMP"}) {
      $curr_cmp_opt = $cmp_opt." ".${$perl_scalar}{$keys}{"MY-CMP"};
    } else {
      $curr_cmp_opt = $cmp_opt;
    }
    push @valid_file_list, &find_valid_filelist($dst_full_path{$keys});
    my @svlg_file_list=();
    my @vlog_file_list=();
    my @vhdl_file_list=();
    foreach my $flist_name (@valid_file_list) {
      my @rtl_file_list=();
      push @rtl_file_list, &remove_flist_variable($flist_name);
      if($flist_name =~ /(_sim|_rtl|_model)_svlg/) {
        print "  #SystemVerilog Filelist: $flist_name\n";
        push @svlg_file_list, @rtl_file_list;
      } elsif($flist_name =~ /(_sim|_rtl|_model)_vhdl/) {
        print "  #VHDL Filelist:$flist_name\n";
        push @vhdl_file_list, @rtl_file_list;
      } else {
        print "  #Verilog Filelist:$flist_name\n";
        push @vlog_file_list, @rtl_file_list;
      }
    }
    print "\@svlg_file_list = ", scalar(@svlg_file_list)."\n";
    print "\@vhdl_file_list = ", scalar(@vhdl_file_list)."\n";
    print "\@vlog_file_list = ", scalar(@vlog_file_list)."\n";
    #@svlg_file_list = grep {++$hash{$_}<2} @svlg_file_list;
    if(scalar(@svlg_file_list) != 0) {
      my $cmp_file_list = $cmp_dir."/".$keys."_svlg.lst";
      my $cmp_log       = $keys."_svlg_comp.log";
      push @stored_log_file_list, $cmp_dir."/".$cmp_log;
      &filelist_gen($cmp_file_list, \@svlg_file_list);
      my $cmd = sprintf("star_base -cmp_dir %-s -comp -comp_opt \"-sverilog -file %-s %-s\" -comp_log \"%s\"", $cmp_dir, $cmp_file_list, $curr_cmp_opt, $cmp_log);
      print $cmd."\n";
      my $result = system($cmd);
      if($result != 0) {
        print color("bold red");
        print("ERROR: $cmp_file_list compile FAIL!!!\n");
        print color("clear reset");
        exit(1);
      }
    }
    #@vlog_file_list = grep {++$hash{$_}<2} @vlog_file_list;
    if(scalar(@vlog_file_list) != 0) {
      my $cmp_file_list = $cmp_dir."/".$keys."_vlog.lst";
      my $cmp_log       = $keys."_vlog_comp.log";
      push @stored_log_file_list, $cmp_dir."/".$cmp_log;
      &filelist_gen($cmp_file_list, \@vlog_file_list);
      my $cmd = sprintf("star_base -cmp_dir %-s -comp -comp_opt \"-v2005 -file %-s %-s\" -comp_log \"%s\"", $cmp_dir, $cmp_file_list, $curr_cmp_opt, $cmp_log);
      print $cmd."\n";
      my $result = system($cmd);
      if($result != 0) {
        print color("bold red");
        print("ERROR: $cmp_file_list compile FAIL!!!\n");
        print color("clear reset");
        exit(1);
      }

    }
    #@vhdl_file_list = grep {++$hash{$_}<2} @vhdl_file_list;
    if(scalar(@vhdl_file_list) != 0) {
      &filelist_gen($cmp_dir."/".$keys."_vhdl.lst", \@vhdl_file_list);
      my $cmp_file_list = $cmp_dir."/".$keys."_vhdl.lst";
      my $cmp_log       = $keys."_vhdl_comp.log";
      push @stored_log_file_list, $cmp_dir."/".$cmp_log;
      &filelist_gen($cmp_file_list, \@vlog_file_list);
      my $cmd = sprintf("star_base -vhdl -cmp_dir %-s -comp -comp_opt \"-file %-s %-s\" -comp_log \"%s\"", $cmp_dir, $cmp_file_list, $curr_cmp_opt, $cmp_log);
      print $cmd."\n";
      my $result = system($cmd);
      if($result != 0) {
        print color("bold red");
        print("ERROR: $cmp_file_list compile FAIL!!!\n");
        print color("clear reset");
        exit(1);
      }

    }
  }
}

@valid_file_list=();
if (scalar(@rtl_file_list_queue) != 0) {
  print color('bold green on_black');
  printf("[%-s]:\n", "RTL File List");
  print color('reset');
  print color('clear');
  foreach my $rtl_file_list (@rtl_file_list_queue) {
    $rtl_file_list = &swap_env_path($rtl_file_list);
  
    push @valid_file_list, $rtl_file_list;
  }
  
  my @svlg_file_list=();
  my @vlog_file_list=();
  my @vhdl_file_list=();
  foreach my $file_list_path (@valid_file_list) {
    my $flist_name = $file_list_path;
    my @rtl_file_list=();
    push @rtl_file_list, &remove_flist_variable($file_list_path);
    print "file_list_path=$file_list_path\n";
    print "@rtl_file_list\n";
    if($flist_name =~ /(_sim|_rtl|_model)_svlg/) {
      print "  #SystemVerilog Filelist:\n";
      push @svlg_file_list, @rtl_file_list;
    } elsif($flist_name =~ /(_sim|_rtl|_model)_vhdl/) {
      print "  #VHDL Filelist:\n";
      push @vhdl_file_list, @rtl_file_list;
    } else {
      print "  #Verilog Filelist:\n";
      push @vlog_file_list, @rtl_file_list;
    }
    my $rtl_file_name = (split('\.', (split('/', $file_list_path))[-1]))[0];
    print "rtl_file_name = $rtl_file_name\n";
    if(scalar(@svlg_file_list) != 0) {
      #@svlg_file_list = grep {++$hash{$_}<2} @svlg_file_list;
      my $cmp_file_list = $cmp_dir."/".$rtl_file_name.".lst";
      my $cmp_log       = $rtl_file_name."_comp.log";
      push @stored_log_file_list, $cmp_dir."/".$cmp_log;
      &filelist_gen($cmp_file_list, \@svlg_file_list);
      my $cmd = sprintf("star_base -cmp_dir %-s -comp -comp_opt \"-sverilog -file %-s %-s\" -comp_log \"%s\"", $cmp_dir, $cmp_file_list, $cmp_opt, $cmp_log);
      print $cmd."\n";
      my $result = system($cmd);
      if($result != 0) {
        print color("bold red");
        print("ERROR: $cmp_file_list compile FAIL!!!\n");
        print color("clear reset");
        exit(1);
      }

    }
    if(scalar(@vlog_file_list) != 0) {
      #@vlog_file_list = grep {++$hash{$_}<2} @vlog_file_list;
      my $cmp_file_list = $cmp_dir."/".$rtl_file_name.".lst";
      my $cmp_log       = $rtl_file_name."_comp.log";
      push @stored_log_file_list, $cmp_dir."/".$cmp_log;
      &filelist_gen($cmp_file_list, \@vlog_file_list);
      my $cmd = sprintf("star_base -cmp_dir %-s -comp -comp_opt \"-v2005 -file %-s %-s\" -comp_log \"%s\"", $cmp_dir, $cmp_file_list, $cmp_opt, $cmp_log);
      print $cmd."\n";
      my $result = system($cmd);
      if($result != 0) {
        print color("bold red");
        print("ERROR: $cmp_file_list compile FAIL!!!\n");
        print color("clear reset");
        exit(1);
      }

    }
    if(scalar(@vhdl_file_list) != 0) {
      #@vhdl_file_list = grep {++$hash{$_}<2} @vhdl_file_list;
      my $cmp_file_list = $cmp_dir."/".$rtl_file_name.".lst";
      my $cmp_log       = $rtl_file_name."_comp.log";
      &filelist_gen($cmp_file_list, \@vhdl_file_list);
      push @stored_log_file_list, $cmp_dir."/".$cmp_log;
      my $cmd = sprintf("star_base -vhdl -cmp_dir %-s -comp -comp_opt \"-file %-s %-s\" -comp_log \"%s\"", $cmp_dir, $cmp_file_list, $cmp_opt, $cmp_log);
      print $cmd."\n";
      my $result = system($cmd);
      if($result != 0) {
        print color("bold red");
        print("ERROR: $cmp_file_list compile FAIL!!!\n");
        print color("clear reset");
        exit(1);
      }

    }
  }
}

&analyze_log_file($cmp_dir."/".$sum_log_name, @stored_log_file_list);
#open RTL_FLIST, ">files_rtl.f";
#@rtl_file_list = grep {++$hash{$_}<2} @rtl_file_list;
#foreach my $file_name (@rtl_file_list) {
#  print RTL_FLIST "$file_name\n";
#}
#close(RLT_FLIST);

sub filelist_gen {
  my $file_list_path = shift @_;
  my $real_file_list_ref = shift @_;
  if(!open TMP_FILELIST, ">$file_list_path") {
    print "Can't open $file_list_path:$!\n";
    exit(1)
  };
     foreach my $file (@{$real_file_list_ref}) {
        if($file =~ /^\s*$/ or $file =~ /^\s*\/\/.*$/) {
        } else {
           print TMP_FILELIST $file, "\n";
        }
     }
    close TMP_FILELIST;
}

sub remove_flist_variable {
    my $flist_path       = shift @_;
    my $cur_dir;
    if($flist_path =~ /^\s*(\S+)\/(\S+)\s*$/) {
      $cur_dir = $1;
    }
    my $warn_info;
    if(!(open INFILE, $flist_path)) {
        $warn_info = "Can't open $flist_path:$!\n";
        push @warn_msg_queue, $warn_info;
        warn $warn_info;
    }
    my @full_path_file_list = ();
    my @line=<INFILE>;
    close INFILE;


    foreach my $cur_line (@line) {
        chomp($cur_line);
        if($cur_line =~ /^\s*\/\// || $cur_line =~/^\s*$/) {
        #} elsif($cur_line =~ /^(.*)\$\{?path_(\w+)\}?(.*)$/){
        #} elsif($cur_line =~ /^(.*)\$\{?(\S+)\}?(.*)$/){
        } elsif($cur_line =~ /(.*)\$\{?(\w+)\}?(.*)/){
            my $remove_line;
            $remove_line = $cur_line;
            do {
              if($remove_line =~ /(.*)\$\{?(\w+)\}?(.*)/) {
                my $line_head = $1;
                my $path_env  = $2;
                my $line_end  = $3;
                my @path_env_list = grep {$_ eq $path_env} (keys %ENV);
                #print "@path_env_list\n";
                if(scalar(@path_env_list) == 0) {
                  print " Error: Can't find Environment variable $path_env at $flist_path\n";
                  exit(1);
                } else {
                  # print(" Debug: \$\{$path_env\} -> $ENV{$path_env} \n");
                    $remove_line = $line_head.$ENV{$path_env}.$line_end;
                }
              }
            } while($remove_line =~ /(.*)\$\{?(\w+)\}?(.*)/);
            if($remove_line =~ /^\s*\-f\s+(\S+)/) {
                my $tmp_flist_path = $1;
                my $next_dir;
                if($tmp_flist_path =~/^\s*(\.|\.\.)\//) {
                  $tmp_flist_path = $cur_dir.'/'.$tmp_flist_path;
                }
                if($tmp_flist_path =~ /^\s*(\S+)\/\w+(\s*$|\s+)/) {
                    $next_dir = $1;
                } else {
                    $next_dir = $cur_dir;
                }
                #push @valid_file_list, &remove_flist_variable($tmp_flist_path);
                push @full_path_file_list, &remove_flist_variable($tmp_flist_path);
            } elsif($remove_line =~ /\.(c|cpp|c\+\+)\s*(\/\/.*)?$/) {
                if($remove_line =~/^\s*(\.|\.\.)\//) {
                    $remove_line = $cur_dir.'/'.$remove_line;
                } elsif($remove_line =~/^\s*\+incdir\+(\.|\.\.)(\/.*)$/) {
                    $remove_line = '+incdir+'.$cur_dir.'/'.$1.$2;
                } elsif($remove_line =~/^\s*(\-y)\s*(\.|\.\.)(\/.*)$/) {
                    $remove_line = $1.$cur_dir.'/'.$2.$3;
                } elsif($remove_line =~/^\s*(\-v)\s*(\.|\.\.)(\/.*)$/) {
                    $remove_line = $cur_dir.'/'.$2.$3;
                } elsif($remove_line =~/^\s*(\-v)\s*(\S+.*)$/) {
                    $remove_line = $2;
                } 
                push @cpp_list, $remove_line;
            } else {
                if($remove_line =~/^\s*(\.|\.\.)\//) {
                    $remove_line = $cur_dir.'/'.$remove_line;
                } elsif($remove_line =~/^\s*\+incdir\+(\.|\.\.)(\/.*)$/) {
                    $remove_line = '+incdir+'.$cur_dir.'/'.$1.$2;
                } elsif($remove_line =~/^\s*(\-y)\s*(\.|\.\.)(\/.*)$/) {
                    $remove_line = $1.$cur_dir.'/'.$2.$3;
                } elsif($remove_line =~/^\s*(\-v)\s*(\.|\.\.)(\/.*)$/) {
                    $remove_line = $cur_dir.'/'.$2.$3;
                } elsif($remove_line =~/^\s*(\-v)\s*(\S+.*)$/) {
                    $remove_line = $2;
                } 
                push @full_path_file_list, $remove_line;
            }
        } else {
            if($cur_line =~ /^\s*\-f\s+(\S+)/) {
                my $tmp_flist_path = $1;
                my $next_dir;
                if($tmp_flist_path =~/^\s*(\.|\.\.)\//) {
                  $tmp_flist_path = $cur_dir.'/'.$tmp_flist_path;
                }
                if($tmp_flist_path =~ /^\s*(\S+)\/\w+(\s*$|\s+)/) {
                  $next_dir = $1;
                } else {
                    $next_dir = $cur_dir;
                }
                #push @valid_file_list, &remove_flist_variable($tmp_flist_path, $cur_dir);
                push @full_path_file_list, &remove_flist_variable($tmp_flist_path);
            } elsif($cur_line =~ /\.(c|cpp|c\+\+)\s*(\/\/.*)?$/) {
                if($cur_line =~/^\s*(\.|\.\.)\//) {
                    $cur_line = $cur_dir.'/'.$cur_line;
                } elsif($cur_line =~/^\s*\+incdir\+(\.|\.\.)(\/.*)$/) {
                    $cur_line = '+incdir+'.$cur_dir.'/'.$1.$2;
                } elsif($cur_line =~/^\s*(\-y)\s*(\.|\.\.)(\/.*)$/) {
                    $cur_line = $1.$cur_dir.'/'.$2.$3;
                } elsif($cur_line =~/^\s*(\-v)\s*(\.|\.\.)(\/.*)$/) {
                    $cur_line = $cur_dir.'/'.$2.$3;
                } elsif($cur_line =~/^\s*(\-v)\s*(\S+.*)$/) {
                    $cur_line = $2;
                } 
                push @cpp_list, $cur_line;
            } else {
                if($cur_line =~/^\s*(\.|\.\.)\//) {
                    $cur_line = $cur_dir.'/'.$cur_line;
                } elsif($cur_line =~/^\s*\+incdir\+(\.|\.\.)(\/.*)$/) {
                    $cur_line = '+incdir+'.$cur_dir.'/'.$1.$2;
                } elsif($cur_line =~/^\s*(\-y)\s*(\.|\.\.)(\/.*)$/) {
                    $cur_line = $1.$cur_dir.'/'.$2.$3;
                } elsif($cur_line =~/^\s*(\-v)\s*(\.|\.\.)(\/.*)$/) {
                    $cur_line = $cur_dir.'/'.$2.$3;
                } elsif($cur_line =~/^\s*(\-v)\s*(\S+.*)$/) {
                    $cur_line = $2;
                } 
                push @full_path_file_list, $cur_line;
            }
        }
    }
    return @full_path_file_list;
}

sub find_valid_filelist{
    my $cur_dir = shift @_;
    $cur_dir = $cur_dir."\/filelist";
    if(!opendir DH, $cur_dir) {
      print "Can't open $cur_dir:$!\n";
      exit(1)
    }
    my @allfiles = grep { $_ ne '.' and $_ ne '..'} readdir DH;
    my @valid_filelist_list=();
    my @fpga_version_filelist=();
    my @fpga_filelist=();
    my @asic_filelist=();
    my @syn_filelist=();
    my @sim_filelist=();
    @allfiles = grep {!/^\./} @allfiles;
    closedir DH;
    foreach my $file (@allfiles) {
        my $file_dir = $cur_dir.'/'.$file;
        if($file =~ /_asic(_sim|_rtl|_model)(_vlog|_vhdl|_svlg)?\.lst$/) {
            push @asic_filelist, $file_dir;
        } elsif ($file =~ /_syn(_vlog|_vhdl|_svlg)?\.lst$/) {
            push @syn_filelist, $file_dir;
        } elsif ($file =~ /_fpga_($fpga_version)(_sim|_rtl|_model)(_vlog|_vhdl|_svlg)?\.lst$/) {
            push @fpga_version_filelist, $file_dir;
        } elsif ($file =~ /_fpga(_sim|_rtl|_model)(_vlog|_vhdl|_svlg)?\.lst$/) {
            push @fpga_filelist, $file_dir;
        } elsif ($file =~ /(_sim|_rtl|_model)(_vlog|_vhdl|_svlg)?\.lst$/) {
            if(!($file =~ /_fpga_/)) {
              push @sim_filelist, $file_dir;
            }
        }
    
        #if(-d $file_dir) {
        #    push @valid_filelist_list, &find_valid_filelist($file_dir);
        #} elsif($file_dir =~ /_sim(_vlog|_vhdl|_svlg)?\.lst$/) {
        #    push @valid_filelist_list, $file_dir;
        #}
    }
    if($fpga_flist == 1) {
        if(scalar(@fpga_version_filelist) != 0) {
          push @valid_filelist_list, @fpga_version_filelist;
        } elsif(scalar(@fpga_filelist) != 0) {
          push @valid_filelist_list, @fpga_filelist;
        } elsif(scalar(@sim_filelist) != 0) {
          push @valid_filelist_list, @sim_filelist;
        } else {
          print "Can't Get FPGA File List in $cur_dir\n";
          exit(1);
        }
    } elsif($syn_flist == 1) {
        if(scalar(@syn_filelist) != 0) {
          push @valid_filelist_list, @syn_filelist;
        } else {
          print "Can't Get FPGA File List in $cur_dir\n";
          exit(1);
        }
    } else {
        if(scalar(@asic_filelist) != 0) {
          push @valid_filelist_list, @asic_filelist;
        } elsif(scalar(@sim_filelist) != 0) {
          push @valid_filelist_list, @sim_filelist;
        } else {
          print "Can't Get Sim File List in $cur_dir\n";
          exit(1);
        }
    }
    print "@valid_filelist_list\n";
    return @valid_filelist_list;
}

sub swap_env_path {
  my $remove_line = shift @_;

  if($remove_line =~ /^(.*)\$\{?(\w+)\}?(.*)$/) {
    my $line_head = $1;
    my $path_env  = $2;
    my $line_end  = $3;
    my @path_env_list = grep {$_ eq $path_env} (keys %ENV);
    #print"@path_env_list\n";
    if(scalar(@path_env_list)==0) {
      print color ('bold red on_black');
      print "ERROR: Can't find Environment variable";
      print color ('bold blue on_black');
      print " \$\{$path_env\}";
      print color ('bold red on_black');
      print "!!!\n";
      print color('reset clear');
      exit(1);
    } else {
      #print("Debug:\$\{$path_env\}->$ENV{$path_env}\n");
      $remove_line=$line_head.$ENV{$path_env}.$line_end;
    }
  }
  return $remove_line;
}

sub grep_file_error_warn {
	my $file_name = shift @_;
	open INFILE, "$file_name";
	my $error_num=0;
	my $warn_num=0;
	while(<INFILE>) {
		my $current_line=$_;
		if($current_line =~ /^\s*Error/) {
			$error_num++;
		}
		if($current_line =~ /^\s*Warning/) {
			$warn_num++;
		}
	}
	return ($error_num, $warn_num);
}

sub analyze_log_file {
  my $summary_file_name = shift @_;
  my @log_file_list = @_;
  my %error_num;
  my %warn_num;
  foreach my $log_file_name (@log_file_list) {
	  ($error_num{$log_file_name}, $warn_num{$log_file_name}) = &grep_file_error_warn($log_file_name);
  }
  if(!open SUM_FILE, ">$summary_file_name") {
    print "  Error: Can't open $summary_file_name:$!\n";
    exit(1)
  }
  my @sorted_keys = sort {$error_num{$a} <=> $error_num{$b}} keys %error_num;
  my $i =1;
  my $total_error=0;
  my $total_warning=0;
  while (scalar @sorted_keys != 0) {
	my $log_name = pop @sorted_keys;
	#print " Debug : $log_name\n";
	#printf ("  Debug: %3d. Error-%3d Warning-%3d (%s)\n",$i, $error_num{$log_name},$warn_num{$log_name},$log_name);
	if($error_num{$log_name}==0) {
	  push @sorted_keys, $log_name;
	  last;
	}
	$total_error = $total_error + $error_num{$log_name};
	$total_warning = $total_warning + $warn_num{$log_name};
	printf SUM_FILE ("  %3d. Error-%3d Warning-%3d (%s)\n",$i, $error_num{$log_name},$warn_num{$log_name},$log_name);
	$i++;
  }
  @sorted_keys = sort {$warn_num{$a} <=> $warn_num{$b}} @sorted_keys;
  while (scalar @sorted_keys != 0) {
	my $log_name = pop @sorted_keys;
	#print " Debug : $log_name\n";
	#printf ("  Debug: %3d. Error-%3d Warning-%3d (%s)\n",$i, $error_num{$log_name},$warn_num{$log_name},$log_name);
	if($warn_num{$log_name}==0) {
	  push @sorted_keys, $log_name;
	  last;
	}
	$total_error = $total_error + $error_num{$log_name};
	$total_warning = $total_warning + $warn_num{$log_name};
	printf SUM_FILE ("  %3d. Error-%3d Warning-%3d (%s)\n",$i, $error_num{$log_name},$warn_num{$log_name},$log_name);
	$i++;
  }
  $total_warning += scalar(@warn_msg_queue);
  foreach my $m_warn_msg (@warn_msg_queue) {
    print color('bold cyan');
	printf ("  WARN: $m_warn_msg\n");
    print color('reset');
    print color('clear');
	printf SUM_FILE ("  WARN: $m_warn_msg\n");
  }
  if($total_error == 0) {
    print color('bold green');
  } else {
    print color('bold red');
  }
  print "  ----------------------------------------------------------\n";
  if($total_error == 0) {
    print "  |             All Compiles PASS!!!                       |\n";
  } else {
    print "  |             All Compiles FAIL!!!                       |\n";
    printf "  |         (Compile Total Error  : %7d)               |\n", $total_error;
  }
  if($total_warning != 0) {
    printf "  |         (Compile Total Warning: %7d)               |\n",$total_warning;
  }
  print "  ----------------------------------------------------------\n";
  print color('reset');
  print color('clear');
  if($total_error != 0 or $total_warning != 0) {
	  print ("  Please ref the following logs for details:\n");
	  print color('bold blue');
	  printf ("         %0s\n", $summary_file_name);
      print color('reset');
      print color('clear');
  }
  if($total_error != 0) {
	  exit(1);
  }
  close SUM_FILE;
}


sub usage() {
    print STDERR << "EOF";
This program is used to resolve dependency at any hierarchy of the IP where 
there is a star_git.cfg file.

usage: $0 [-h] [-f <file_name>]

    -h|--help                 : this (help) message
    -c|--cfg   <file_name>    : Specifies a mb git configuration file that contains a list of git project with JSON format,
                                "MY-SRC" -> git project source 
                                "MY-DST" -> git project's local directory
                                "MY-TAG" -> git project's tag name
                                "BRANCH" -> git project's branch name
                                "MY-CMP" -> git compile options
    -f|--file <file_name>     : rtl filelist, must be full path name
    -cmp_dir  <directory>     : compile directory
    -cmp_opt  <COMP_OPTIONS>  : compile options
    -sum_log  <log_file_name> : it doesn't include the path information, just a name
    -fpga     <fpga_version>  : use fpga version to analysis filelist, \$fpga_version is expected after _fpga_
EOF
    exit;
}




